package com.boot.security.model.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.CredentialsContainer;

import javax.validation.constraints.Pattern;
import java.io.Serializable;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 账户信息
 * @date 2019/11/25 20:38
 **/
@Data
@SuperBuilder
@NoArgsConstructor
@ApiModel("登录账户")
public class LoginAccount implements Serializable, CredentialsContainer {

    @Builder.Default
    @ApiModelProperty(value = "登录方式", name = "type")
    private Integer type = 1;

    @Pattern(regexp = "^[a-zA-Z0-9]{3,16}$", message = "用户名设置3-16位，只包含大小写字母和数字", groups = AccountGroup.UserNameGroup.class)
    @Pattern(regexp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", message = "手机号格式错误", groups = AccountGroup.PhoneGroup.class)
    @Pattern(regexp = "^\\w+((.\\w+)|(-\\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$", message = "邮箱格式错误", groups = AccountGroup.EmailGroup.class)
    @ApiModelProperty(value = "账号", name = "principal")
    private String principal;

    @Pattern(regexp = "^[a-zA-Z0-9_#@]{6,16}$", message = "密码名设置6-16位，可包括大小写字母、数字和3种特殊字符(_、#、@)")
    @ApiModelProperty(value = "凭据", name = "credential")
    private String credential;

    @Builder.Default
    @ApiModelProperty(value = "记住我", name = "rememberMe")
    private Boolean rememberMe = false;

    public static final int USERNAME_PASSWORD = 1;
    public static final int PHONE_PASSWORD = 2;
    public static final int EMAIL_PASSWORD = 3;

    @Override
    public void eraseCredentials() {
        credential = null;
    }
}
